﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portal.Logic;
using Portal.Interface;
using MyLib;
using Portal.Entity;

namespace WebPortal
{
    public partial class Default : System.Web.UI.Page
    {
        public string Default_LogoutConfirm = Resources.lang.Default_LogoutConfirm;
        public string FULL_NAME = "";
        public string DEFAULT_URL = "";
        public string DEFAULT_HEIGHT = ConfigurationManager.AppSettings["FrameHeight"];
        MenuLogic _menu = new MenuLogic();
        IOrg _org = ServiceContainer.Instance().GetService<IOrg>();
        ResourceLogic _resource = new ResourceLogic();

        public string DEFAULT_MYTASK = Resources.lang.DEFAULT_MYTASK;


        protected void Page_Load(object sender, EventArgs e)
        {
           
            //当前菜单Id
            hidId.Value = Request.QueryString["Id"];
            ResourceEntity currentMenu = new ResourceEntity();
            if (string.IsNullOrEmpty(hidId.Value))
            {
                //获取默认菜单为当前菜单
                currentMenu = _menu.GetDefaultMenu(Portal.Logic.SSOLogic.GetLoginName());
                hidId.Value = currentMenu.RESOURCEID.ToString();
            }
            else
            {
                currentMenu = _resource.GetEntity(ConvertUtil.ToInt32(hidId.Value));
            }
            DEFAULT_URL = currentMenu.EXT01;
            if (!string.IsNullOrEmpty(currentMenu.EXT04))
            {
                DEFAULT_HEIGHT = currentMenu.EXT04;
            }

            //获取用户全名
            FULL_NAME = _org.GetUserEntity(Portal.Logic.SSOLogic.GetLoginName()).CNNAME;

            //绑定导航菜单
            List<ResourceEntity> list = new List<ResourceEntity>();
            list = _menu.GetMenuList(Portal.Logic.SSOLogic.GetLoginName());
            List<ResourceEntity> root = new List<ResourceEntity>();
            root = list.FindAll(p => p.PARENTID == 0);
            int i = 0;
            foreach (ResourceEntity ety in root)
            {
                ety.EXT30 = "";
                if (ety.RESOURCEID==currentMenu.RESOURCEID) //默认页
                {
                    ety.EXT30 = "act";
                }

                TreeNode tn = new TreeNode();
                if (Portal.Resources.lang.ResourceNameField.ToUpper() == "CNNAME")
                {
                    tn.Text = ety.CNNAME;
                }
                else
                {
                    tn.Text = ety.ENNAME;
                }
                tn.Value = ety.RESOURCEID.ToString();
                tn.NavigateUrl = "#";
                tn.ImageUrl = ety.EXT05;
                if (i > 0)
                {
                    tn.Collapse();
                }
                TreeView1.Nodes.Add(tn);
                AddChildNodes(list,tn, ety.RESOURCEID);
                i++;
            }

        }

        //绑定子菜单
        void AddChildNodes(List<ResourceEntity> alllist ,TreeNode node, int parentID)
        {
            List<ResourceEntity> list = alllist.FindAll(p => p.PARENTID == parentID);
            foreach (ResourceEntity ety in list)
            {
                TreeNode tn = new TreeNode();
                if (Portal.Resources.lang.ResourceNameField.ToUpper() == "CNNAME")
                {
                    tn.Text = ety.CNNAME;
                }
                else
                {
                    tn.Text = ety.ENNAME;
                }
                tn.Value = ety.RESOURCEID.ToString();
                if (!string.IsNullOrEmpty(ety.EXT01))
                {
                    tn.NavigateUrl = ety.EXT01;
                }
                else
                {
                    tn.NavigateUrl = "#";
                }
                tn.Target = ety.EXT02;
                if (!string.IsNullOrEmpty(ety.EXT05))
                {
                    tn.ImageUrl = ety.EXT05;
                }
                else
                {
                    tn.ImageUrl = "Custom/Default/images/icon-menu.gif";
                }
                node.ChildNodes.Add(tn);

                AddChildNodes(alllist,tn, ety.RESOURCEID);
            }
        }


    }
}